﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using ECS.Framework;
using ECS.BLL;
using ECS.BLL.Component;
using ECS.BLL.Entity;

namespace E_ICS.E_ICS
{
    public partial class frmAgentSearch : Form
    {
        /// <summary>
        /// ตัวแปร
        /// </summary>
        #region private properties
        private string _agentCode = string.Empty;
        private string _agentName = string.Empty;
        private string _Branch = string.Empty;
        private string _Address = string.Empty;
        private string _City = string.Empty;
        private string _ewsRequired = string.Empty;
        private string _selAgentCode = string.Empty;
        private string _selAgentName = string.Empty;
        private string _selEws = string.Empty;
        private DataSet _dsAgent = new DataSet();
        private Main.frmMain2 main=null;      
        #endregion

        #region form events

        public frmAgentSearch(Main.frmMain2 _main)
        {
            InitializeComponent();
            main = _main;
        }
        /// <summary>
        /// Load Resourse From Database to GUI
        /// </summary>
         private void SetScreen()
        {
            this.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_Name");
            label3.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_label3");
            label1.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_label1");
            btnSearch.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_btnSearch");
            btnClear.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_btnClear");
            btnSave.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_btnSave");
            btnClose.Text = CommonResourceString.GetResourseValue("scn_AgentSearch_btnClose");               
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                SearchAgent();
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                // string[] array = new string[3] { SelAgentCode, SelAgentName, SelEws };
                ValueTofrmAddMAWB.SelAgentCode = _selAgentCode;
                ValueTofrmAddMAWB.SelAgentName = _selAgentName;
                ValueTofrmAddMAWB.SelEws = _selEws;
                if (ViewEditWeightSlip.flagfromViewEditWeightSlip == 1)
                {
                    this.Dispose();
                    this.DialogResult = DialogResult.Yes;
                    _selAgentCode = string.Empty;
                    _selAgentName = string.Empty;
                    _selEws = string.Empty;
                }
                else
                {
                    //main.GetDataFromAgentSearch(array);
                    // frmEditMAWBInfo fm = new frmEditMAWBInfo(main);
                    //fm.GetDataFromAgentSearch(array);
                    //fm.ShowDialog();
                    this.Dispose();
                    this.DialogResult = DialogResult.Yes;
                    _selAgentCode = string.Empty;
                    _selAgentName = string.Empty;
                    _selEws = string.Empty;
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }
       
        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                int index = e.RowIndex;
                int columnIndex = e.ColumnIndex;
                if (columnIndex < 0)
                {
                    columnIndex = 0; 
                }
                if (e.RowIndex >= 0)
                {
                    //dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
                    dgvAgent.Rows[index].Selected = true;
                    if (dgvAgent.Rows[e.RowIndex].Cells[columnIndex].Value != null && e.RowIndex != -1)
                    {
                        _selAgentCode = dgvAgent.Rows[e.RowIndex].Cells[0].Value.ToString();
                        _selAgentName = dgvAgent.Rows[e.RowIndex].Cells[1].Value.ToString();
                        _selEws = dgvAgent.Rows[e.RowIndex].Cells[5].Value.ToString();
                        ViewEditWeightSlip.Agent = _selAgentCode;
                        ViewEditWeightSlip.Agent_Name = _selAgentName;

                    }
                }
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
                //Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
        }
        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            try
            {
                dgvAgent.ClearSelection();
                _selAgentCode = string.Empty;
                _selAgentName = string.Empty;
                _selEws = string.Empty;
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                dgvAgent.DataSource = null;
                txtAgentCode.Text = string.Empty;
                txtAgentName.Text = string.Empty;
            }
            catch (Exception ex)
            {
                CommonMessageBox.ShowMessageError(ex);
            }

        }
        private void txtAgentCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    SearchAgent();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }
        }

        private void txtAgentName_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                if (e.KeyChar == (char)Keys.Enter)
                {
                    SearchAgent();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message + "\r\n" + ex.StackTrace);
            }

        }
        #endregion

        #region private methods

        private DataTable CreateAgentDataTable(DataSet dsInput)
        {
            DataTable dtAgent = new DataTable();
            ///////////create head column/////////////////
            dtAgent.Columns.Add("Agent Code", typeof(string));
            dtAgent.Columns.Add("Agent Name", typeof(string));
            dtAgent.Columns.Add("Branch", typeof(string));
            dtAgent.Columns.Add("Address", typeof(string));
            dtAgent.Columns.Add("City", typeof(string));
            dtAgent.Columns.Add("ews_required", typeof(string));
            ///////////////////////////////////////////////
            int row = dsInput.Tables[0].Rows.Count;
            for (int i = 0; i < row; i++)
            {
                _agentCode = dsInput.Tables[0].Rows[i]["agent_code"].ToString().Trim();
                _agentName = dsInput.Tables[0].Rows[i]["agent_name"].ToString().Trim();
                _Branch = dsInput.Tables[0].Rows[i]["branch"].ToString().Trim();
                _Address = dsInput.Tables[0].Rows[i]["address"].ToString().Trim();
                _City = dsInput.Tables[0].Rows[i]["city"].ToString().Trim();
                _ewsRequired = dsInput.Tables[0].Rows[i]["ews_required"].ToString().Trim();
                dtAgent.Rows.Add(_agentCode, _agentName, _Branch, _Address, _City, _ewsRequired);
            }
            return dtAgent;
        }
        /// <summary>
        /// search agent with Code AND/OR Name
        /// </summary>
        private void SearchAgent()
        {
            dgvAgent.DataSource = null;
            SearchInfo search = new SearchInfo();
            SearchCriteria criteria = new SearchCriteria();
            criteria.AppId = GlobalVariable.user.ApplicationID;
            criteria.UserId = GlobalVariable.user.UserID;
            criteria.Device = GlobalVariable.user.Device;
            criteria.AgentCode = txtAgentCode.Text.Trim();
            criteria.AgentName = txtAgentName.Text.Trim();
            _dsAgent = new DataSet();
            _dsAgent = search.AgentSearch(criteria);
            if (_dsAgent.Tables[0].Rows.Count > 0)
            {
                string status = _dsAgent.Tables[0].Rows[0]["Status"].ToString();
                string message = _dsAgent.Tables[0].Rows[0]["Message"].ToString();
                if (status == "PASS")
                {
                    DataTable Agent_TB = CreateAgentDataTable(_dsAgent);
                    dgvAgent.DataSource = Agent_TB;
                    dgvAgent.Rows[0].Selected = true;
                    _selAgentCode = dgvAgent.Rows[0].Cells[0].Value.ToString();
                    _selAgentName = dgvAgent.Rows[0].Cells[1].Value.ToString();
                    _selEws = dgvAgent.Rows[0].Cells[5].Value.ToString();
                }
                else
                {
                    CommonMessageBox.ShowMessageWarning(message);
                }
            }
        }

        private void frmAgentSearch_Load(object sender, EventArgs e)
        {
            SetScreen();
            txtAgentCode.Focus();
        }
        #endregion
    }
}
